home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1996 / MacHack 1996.toast / Hacks / Hacks ’95 / Menu Controls / UShapesDocument.cp < prev    next >
Encoding:
Text File  |  1995-06-24  |  28.7 KB  |  997 lines  |  [TEXT/MPS ]

  1. // Copyright © 1994-95 by Apple Computer, Inc. All rights reserved.
  2. // UDrawShapes.cp
  3.  
  4. #ifndef __UOBJECT__
  5. #include <UObject.h>    // work around nested include limitation in CFront
  6. #endif
  7.  
  8. #ifndef __USHAPESDOCUMENT__
  9. #include "UShapesDocument.h"
  10. #endif
  11.  
  12. // MacApp
  13.  
  14. #ifndef __UMENUMGR__
  15. #include <UMenuMgr.h>
  16. #endif
  17.  
  18. #ifndef __UPRINTING__
  19. #include <UPrinting.h>
  20. #endif
  21.  
  22. #ifndef __USCROLLER__
  23. #include <UScroller.h>
  24. #endif
  25.  
  26. #ifndef __UVIEWSERVER__
  27. #include <UViewServer.h>
  28. #endif
  29.  
  30. #ifndef __UWINDOW__
  31. #include <UWindow.h>
  32. #endif
  33.  
  34. #ifndef __USTREAM__
  35. #include <UStream.h>
  36. #endif
  37.  
  38. #ifndef __UMACAPPUTILITIES__
  39. #include <UMacAppUtilities.h>
  40. #endif
  41.  
  42. #ifndef __UMACAPPGLOBALS__
  43. #include <UMacAppGlobals.h>
  44. #endif
  45.  
  46. // Toolbox
  47.  
  48. #ifndef __TOOLUTILS__
  49. #include <ToolUtils.h>
  50. #endif
  51.  
  52. // DrawShapes
  53.  
  54. #ifndef __SHAPECOMMANDS__
  55. #include "ShapeCommands.h"
  56. #endif
  57.  
  58. #ifndef __UDRAWSHAPES__
  59. #include "UDrawShapes.h"
  60. #endif
  61.  
  62. #ifndef __USHAPES__
  63. #include "UShapes.h"
  64. #endif
  65.  
  66. #ifndef __USHAPEVIEW__
  67. #include "UShapeView.h"
  68. #endif
  69.  
  70. //----------------------------------------------------------------------------------------
  71. // Resources
  72.  
  73. // Menu bars for color and for b&w systems
  74. #define kColorMenuBar        131
  75. #define kNonColorMenuBar    132
  76.  
  77. #define kRainbowArrow        140
  78.  
  79. #define kPickerPrompt        256
  80.  
  81. const OSType kDocRsrcKind        = 'DSTA';    // Resource type for document state
  82. const short  kDocStateID        = 1;        // Resource id for document state
  83.  
  84. // Windows
  85.  
  86. const short kShapeWindowRSRCID    = 1005;        // Windows used by documents in the application
  87.  
  88. const short kShapeViewRSRCID    = 1006;        // Resource id for the shape view template,
  89.                                             // used when printing & using template views
  90.  
  91. const short kToolsPaletteRSRCID = 1007;        // Resource id for the floating window with
  92.                                             // tools palette
  93.  
  94. const short kPatternsPaletteRSRCID = 1008;    // Resource id for the floating window with
  95.                                             // patterns palette
  96.  
  97. // Menus
  98. const short mTools            = 5;            // the Tools menu resource id
  99. const short mPatterns        = 6;            // the Patterns menu resource id
  100. const short mColor            = 7;            // the Color menu resource id
  101. const short mMoreDebug        = 8;            // Menu number for the "More Debug" menu
  102.  
  103.  
  104. // Command Numbers
  105.  
  106. #define cArrow    1100
  107. #define cBox    1101
  108. #define cCircle    1102
  109. #define cHBox    1103
  110.  
  111. //----------------------------------------------------------------------------------------
  112. // Miscellaneous
  113.  
  114. const short kStaggerAmount = 16;            // Amount to stagger windows by
  115.  
  116.  
  117. //========================================================================================
  118. // Functions called by FirstThat
  119. // typedef Boolean (*TestShapeType)(TShape* item, void* staticLink);
  120.  
  121. //----------------------------------------------------------------------------------------
  122. #pragma segment ShapeRes
  123.  
  124. Boolean IsShapeSelected(TShape* shape, void* /*staticLink*/)
  125. {
  126.     return shape->IsSelected();
  127. }
  128.  
  129. //========================================================================================
  130. // Functions called by Each, EachShapeDo, etc.
  131. // typedef void(* DoToShapeType)(TShape* item, void* staticLink);
  132.  
  133. //----------------------------------------------------------------------------------------
  134. struct CheckShapeRec
  135. {
  136.     CPoint&    fqdPt;
  137.     TShape*    fShapeUnderMouse;
  138.  
  139.     CheckShapeRec(CPoint& qdPt);
  140. };
  141.  
  142. #pragma segment ASelCommand
  143.  
  144. CheckShapeRec::CheckShapeRec(CPoint& qdPt) :
  145.     fqdPt(qdPt)
  146. {
  147.     fShapeUnderMouse = NULL;
  148. }
  149.  
  150. //----------------------------------------------------------------------------------------
  151. #pragma segment ASelCommand
  152.  
  153. void CheckShape(TShape* shape, void* staticLink)
  154. {
  155.     // staticLink is really a CheckShapeRec*
  156.     CheckShapeRec* checkShapePtr = (CheckShapeRec*)staticLink;
  157.     CRect extent;
  158.     shape->GetFrame(extent);
  159.     if (PtInRect(checkShapePtr->fqdPt, extent))
  160.         checkShapePtr->fShapeUnderMouse = shape;
  161. }
  162.  
  163. //----------------------------------------------------------------------------------------
  164. struct DrawShapeRec
  165. {
  166.     CRect&    fqdArea;
  167.     Boolean    fDragging;
  168.  
  169.     DrawShapeRec(CRect& qdArea, Boolean dragging);
  170. };
  171.  
  172. #pragma segment ARes
  173.  
  174. DrawShapeRec::DrawShapeRec(CRect& qdArea, Boolean dragging) :
  175.     fqdArea(qdArea),
  176.     fDragging(dragging)
  177. { }
  178.  
  179. //----------------------------------------------------------------------------------------
  180. #pragma segment ARes
  181.  
  182. void DrawShape(TShape* shape, void* staticLink)
  183. {
  184.     // staticLink is really a DrawShapeRec*
  185.     DrawShapeRec* drawShapePtr = (DrawShapeRec*)staticLink;
  186.  
  187.     CRect r;
  188.     CRect extent;
  189.     shape->GetFrame(extent);
  190.     if (!(drawShapePtr->fDragging && shape->IsSelected()) &&
  191.         SectRect(extent, drawShapePtr->fqdArea, r))
  192.             shape->Draw();
  193. }
  194.  
  195. //----------------------------------------------------------------------------------------
  196. #pragma segment AClose
  197.  
  198. void FreeIfShape(TShape* shape, void* /*staticLink*/)
  199. {
  200.     FreeIfObject(shape);
  201. }
  202.  
  203. //----------------------------------------------------------------------------------------
  204. #pragma segment AWriteFile
  205.  
  206. void WriteShape(TShape* shape, void* staticLink)
  207. {
  208.     // staticLink is really a TStream*
  209.     TStream* aStream = (TStream*)staticLink;
  210.  
  211.     aStream->WriteStreamObject(shape, kNoStandardObject);
  212. }
  213.  
  214. //----------------------------------------------------------------------------------------
  215. #pragma segment ShapeRes
  216.  
  217. void DeselShape(TShape* shape, void* /*staticLink*/)
  218. {
  219.     shape->SetSelected(false);
  220. }
  221.  
  222. //----------------------------------------------------------------------------------------
  223. #pragma segment ShapeRes
  224.  
  225. void DoRestore(TShape* shape, void* staticLink)
  226. {
  227.     // staticLink is really a TShapeView*
  228.     TShapeView* shapeView = (TShapeView*)staticLink;
  229.  
  230.     Boolean wantInval = shape->IsSelected() || shape->WasSelected();
  231.     shape->SetIsWasSelected();
  232.     if (wantInval)
  233.         shapeView->InvalShape(shape);
  234. }
  235.  
  236. //----------------------------------------------------------------------------------------
  237. struct HiliteShapeRec
  238. {
  239.     TShapeView*    fShapeView;
  240.     HLState        fFromHL;
  241.     HLState        fToHL;
  242.  
  243.     HiliteShapeRec(TShapeView* shapeView, HLState fromHL, HLState toHL);
  244. };
  245.  
  246. #pragma segment ShapeRes
  247.  
  248. HiliteShapeRec::HiliteShapeRec(TShapeView* shapeView, HLState fromHL, HLState toHL) :
  249.     fShapeView(shapeView),
  250.     fFromHL(fromHL),
  251.     fToHL(toHL)
  252. { }
  253.  
  254. //----------------------------------------------------------------------------------------
  255. #pragma segment ShapeRes
  256.  
  257. void HiliteShape(TShape* shape, void* staticLink)
  258. {
  259.     // staticLink is really a HiliteShapeRec*
  260.     HiliteShapeRec* hlRecPtr = (HiliteShapeRec*)staticLink;
  261.  
  262.     if (shape->IsSelected() && !hlRecPtr->fShapeView->IsDragging())
  263.         shape->Highlight(hlRecPtr->fFromHL, hlRecPtr->fToHL, hlRecPtr->fShapeView);
  264. }
  265.  
  266. //----------------------------------------------------------------------------------------
  267. struct SaveSelRec
  268. {
  269.     TShapeView*    fShapeView;
  270.     Boolean        fAndInval;
  271.  
  272.     SaveSelRec(TShapeView* shapeView, Boolean andInval);
  273. };
  274.  
  275. #pragma segment ShapeRes
  276.  
  277. SaveSelRec::SaveSelRec(TShapeView* shapeView, Boolean andInval) :
  278.     fShapeView(shapeView),
  279.     fAndInval(andInval)
  280. { }
  281.  
  282. //----------------------------------------------------------------------------------------
  283. #pragma segment ShapeRes
  284.  
  285. void SaveSel(TShape* shape, void* staticLink)
  286. {
  287.     // staticLink is really a SaveSelRec*
  288.     SaveSelRec* selRecPtr = (SaveSelRec*)staticLink;
  289.  
  290.     shape->SetWasIsSelected();
  291.     if (shape->WasSelected() && selRecPtr->fAndInval)
  292.         selRecPtr->fShapeView->InvalShape(shape);
  293. }
  294.  
  295. //----------------------------------------------------------------------------------------
  296. #pragma segment ASelCommand
  297.  
  298. void SelectIt(TShape* shape, void* staticLink)
  299. {
  300.     // staticLink is really a TShapeView*
  301.     TShapeView* shapeView = (TShapeView*)staticLink;
  302.  
  303.     if (!shape->IsSelected())
  304.     {
  305.         shape->SetSelected(true);
  306.         shape->Highlight(hlOff, hlOn, shapeView);
  307.     }
  308. }
  309.  
  310. //----------------------------------------------------------------------------------------
  311. struct TestShapesRec
  312. {
  313.     Boolean&    fAnySelection;
  314.     Boolean&    fAnyShapes;
  315.     Boolean        fDocFiltering;
  316.  
  317.     TestShapesRec(Boolean& anySelection, Boolean& anyShapes, Boolean docFiltering);
  318. };
  319.  
  320. #pragma segment ARes
  321.  
  322. TestShapesRec::TestShapesRec(Boolean& anySelection, Boolean& anyShapes, Boolean docFiltering) :
  323.     fAnySelection(anySelection),
  324.     fAnyShapes(anyShapes),
  325.     fDocFiltering(docFiltering)
  326. { }
  327.  
  328. //----------------------------------------------------------------------------------------
  329. #pragma segment ARes
  330.  
  331. void TestShapes(TShape* shape, void* staticLink)
  332. {
  333.     // staticLink is really a TestShapesRec*
  334.     TestShapesRec* testShapesPtr = (TestShapesRec*)staticLink;
  335.  
  336.     testShapesPtr->fAnySelection |= shape->IsSelected();
  337.     testShapesPtr->fAnyShapes |= testShapesPtr->fDocFiltering;
  338.     testShapesPtr->fAnyShapes |= shape->WasSelected();
  339. }
  340.  
  341. //----------------------------------------------------------------------------------------
  342. struct TestCursorRec
  343. {
  344.     Boolean&    fCursorSet;
  345.     CPoint        fqdPoint;
  346.     RgnHandle    fCursorRegion;
  347.  
  348.     TestCursorRec(Boolean& cursorSet, CPoint qdPoint, RgnHandle cursorRegion);
  349. };
  350.  
  351. #pragma segment ShapeRes
  352.  
  353. TestCursorRec::TestCursorRec(Boolean& cursorSet, CPoint qdPoint, RgnHandle cursorRegion) :
  354.     fCursorSet(cursorSet),
  355.     fqdPoint(qdPoint),
  356.     fCursorRegion(cursorRegion)
  357. { }
  358.  
  359. //----------------------------------------------------------------------------------------
  360. #pragma segment ShapeRes
  361.  
  362. void TestShapeCursor(TShape* shape, void* staticLink)
  363. {
  364.     // staticLink is really a TestCursorRec*
  365.     TestCursorRec* testPtr = (TestCursorRec*)staticLink;
  366.  
  367.     if (!testPtr->fCursorSet)
  368.     {
  369.         CRect extent;
  370.         shape->GetFrame(extent);
  371.         if (PtInRect(testPtr->fqdPoint, extent))
  372.         {
  373.             UseROMMap(true);
  374.             SetCursor(*GetCursor(plusCursor));
  375.             RectRgn(testPtr->fCursorRegion, extent);
  376.             testPtr->fCursorSet = true;
  377.         }
  378.     }
  379. }
  380.  
  381. //----------------------------------------------------------------------------------------
  382. struct SurveyRec
  383. {
  384.     Boolean    fSelecteesOnly;
  385.     short&    fNumberOfShapes;
  386.     CRect&    fCombinedExtent;
  387.  
  388.     SurveyRec(Boolean selecteesOnly, short& numberOfShapes, CRect& combinedExtent);
  389. };
  390.  
  391. SurveyRec::SurveyRec(Boolean selecteesOnly, short& numberOfShapes, CRect& combinedExtent) :
  392.     fSelecteesOnly(selecteesOnly),
  393.     fNumberOfShapes(numberOfShapes),
  394.     fCombinedExtent(combinedExtent)
  395. { }
  396.  
  397. //----------------------------------------------------------------------------------------
  398. #pragma segment ShapeRes
  399.  
  400. void UnionizeShapes(TShape* shape, void* staticLink)
  401. {
  402.     // staticLink is really a SurveyRec*
  403.     SurveyRec* surveyPtr = (SurveyRec*)staticLink;
  404.  
  405.     if (shape->IsSelected() || !surveyPtr->fSelecteesOnly)
  406.     {
  407.         CRect extent;
  408.         shape->GetFrame(extent);
  409.         surveyPtr->fNumberOfShapes++;
  410.         if (surveyPtr->fNumberOfShapes > 1)
  411.             UnionRect(extent, surveyPtr->fCombinedExtent, surveyPtr->fCombinedExtent);
  412.         else
  413.             surveyPtr->fCombinedExtent = extent;
  414.     }
  415. }
  416.  
  417. //----------------------------------------------------------------------------------------
  418. #pragma segment ARes
  419.  
  420. void ShapeBeInView(TShape* shape, void* staticLink)
  421. {
  422.     shape->BeInView((TShapeView*)staticLink);
  423. }
  424.  
  425. //========================================================================================
  426. // CLASS TShapeDocument
  427. //========================================================================================
  428. #undef Inherited
  429. #define Inherited TFileBasedDocument
  430.  
  431. #pragma segment AOpen
  432. MA_DEFINE_CLASS_M1(TShapeDocument, Inherited);
  433.  
  434. //----------------------------------------------------------------------------------------
  435. // TShapeDocument Constructor
  436. //----------------------------------------------------------------------------------------
  437. #pragma segment AOpen
  438.  
  439. TShapeDocument::TShapeDocument()
  440. {
  441.     fShapeView = NULL;
  442.     fShapeList = NULL;                                    
  443.  
  444.     fSavePrintInfo = true;
  445.     fSaveUserSelection = false;
  446.  
  447.     fReopening = false;
  448.     fFiltering = false;
  449.     fReplaceCommand = NULL;
  450. }
  451.  
  452. //----------------------------------------------------------------------------------------
  453. // TShapeDocument::IShapeDocument
  454. //----------------------------------------------------------------------------------------
  455. #pragma segment AOpen
  456.  
  457. void TShapeDocument::IShapeDocument(TFile* itsFile)
  458. {
  459.     MAVolatileInit(TShapeList*, shapeList, NULL);
  460.     
  461.     this->IFileBasedDocument(itsFile, kShapeClipType);
  462.     
  463.     FailInfo fi;
  464.     Try(fi)
  465.     {
  466.         shapeList = new TShapeList;
  467.         shapeList->IList();
  468.         fShapeList = shapeList;
  469.         fi.Success();
  470.     }
  471.     else
  472.     {
  473.         FreeIfObject(shapeList);
  474.         this->Free();
  475.         fi.ReSignal();
  476.     }
  477. #if qDebug
  478.     fShapeList->SetEltType("TShape");
  479. #endif
  480. }
  481.  
  482. //----------------------------------------------------------------------------------------
  483. // TShapeDocument::Free
  484. //----------------------------------------------------------------------------------------
  485. #pragma segment AClose
  486.  
  487. void TShapeDocument::Free()    // Override
  488. {
  489.     this->FreeData();
  490.     fShapeList = (TShapeList*)FreeListIfObject(fShapeList);
  491.  
  492.     Inherited::Free();
  493. }
  494.  
  495. //----------------------------------------------------------------------------------------
  496. // TShapeDocument::Free
  497. //----------------------------------------------------------------------------------------
  498. #pragma segment AClose
  499.  
  500. void TShapeDocument::FreeData()    // Override
  501. {
  502.     if (fShapeList)
  503.     {
  504.         this->EachShapeDo(&FreeIfShape, NULL);
  505.         fShapeList->DeleteAll();
  506.     }
  507. }
  508.  
  509. //----------------------------------------------------------------------------------------
  510. // TShapeDocument::AddShape
  511. //----------------------------------------------------------------------------------------
  512. #pragma segment ShapeRes
  513.  
  514. void TShapeDocument::AddShape(TShape* shape)
  515. {
  516.     fShapeList->InsertLast(shape);
  517. }
  518.  
  519. //----------------------------------------------------------------------------------------
  520. // TShapeDocument::DeleteShape
  521. //----------------------------------------------------------------------------------------
  522. #pragma segment ShapeRes
  523.  
  524. void TShapeDocument::DeleteShape(TShape* shape)
  525. {
  526.     fShapeList->Delete(shape);
  527.     FreeIfObject(shape);
  528. }
  529.  
  530. //----------------------------------------------------------------------------------------
  531. // TShapeDocument::EachShapeDo
  532. //----------------------------------------------------------------------------------------
  533. #pragma segment ShapeRes
  534.  
  535. void TShapeDocument::EachShapeDo(DoToShapeType DoToShape, void* staticLink)
  536. {
  537.     fShapeList->Each(DoToShape, staticLink);
  538. }
  539.  
  540. //----------------------------------------------------------------------------------------
  541. // TShapeDocument::EachShapeMaybeDo
  542. //----------------------------------------------------------------------------------------
  543. #pragma segment ShapeRes
  544.  
  545. void TShapeDocument::EachShapeMaybeDo(DoToShapeType DoToShape, void* staticLink)
  546. {
  547.     CShapeIterator iter(fShapeList);
  548.  
  549.     for (TShape* shape = iter.FirstShape(); iter.More(); shape = iter.NextShape())
  550.     {
  551.         if (!fFiltering || !shape->WasSelected())
  552.             DoToShape(shape, staticLink);
  553.     }
  554. }
  555.  
  556. //----------------------------------------------------------------------------------------
  557. // TShapeDocument::EachPotentialShapeDo
  558. //----------------------------------------------------------------------------------------
  559. #pragma segment ShapeRes
  560.  
  561. void TShapeDocument::EachPotentialShapeDo(DoToShapeType DoToShape, void* staticLink)
  562. {
  563.     this->EachShapeDo(DoToShape, staticLink);
  564.     if (fReplaceCommand)
  565.         fReplaceCommand->EachNewShapeDo(DoToShape, staticLink);
  566. }
  567.  
  568. //----------------------------------------------------------------------------------------
  569. // TShapeDocument::EachVirtualShapeDo
  570. //----------------------------------------------------------------------------------------
  571. #pragma segment ShapeRes
  572.  
  573. void TShapeDocument::EachVirtualShapeDo(DoToShapeType DoToShape, void* staticLink)
  574. {
  575.     //    EachVirtualShape iterates through only those shapes that appear
  576.     //    to be present at the moment to the USER, given the
  577.     //    UNDO/REDO status of the last command. Thus it iterates
  578.     //    through some but possibly not all of the shapes in the
  579.     //    document, and possibly also through not-yet-in-the-document pastees
  580.  
  581.     this->EachShapeMaybeDo(DoToShape, staticLink);
  582.     if (fReplaceCommand)
  583.         fReplaceCommand->EachNewShapeDo(DoToShape, staticLink);
  584. }
  585.  
  586. //----------------------------------------------------------------------------------------
  587. // TShapeDocument::FirstSelectedShape
  588. //----------------------------------------------------------------------------------------
  589. #pragma segment ShapeRes
  590.  
  591. TShape* TShapeDocument::FirstSelectedShape()
  592. {
  593.     TShape* shape = fShapeList->FirstThat(&IsShapeSelected, NULL);
  594.     return shape;
  595. }
  596.  
  597. //----------------------------------------------------------------------------------------
  598. // TShapeDocument::AddPrintHandlerToView
  599. //----------------------------------------------------------------------------------------
  600. #pragma segment AOpen
  601.  
  602. void TShapeDocument::AddPrintHandlerToView(TView* theView)
  603. {
  604.     TStdPrintHandler* printHandler = new TStdPrintHandler;
  605.     printHandler->IStdPrintHandler(this, theView, !kSquareDots, !kFixedSize, !kFixedSize);
  606. }
  607.  
  608. //----------------------------------------------------------------------------------------
  609. // TShapeDocument::DoMakeViews
  610. //----------------------------------------------------------------------------------------
  611. #pragma segment AOpen
  612.  
  613. void TShapeDocument::DoMakeViews(Boolean forPrinting)    // Override
  614. {
  615.     if (forPrinting)
  616.     {
  617.         if (qTemplateViews)
  618.         {
  619.             TShapeView* shapeView = (TShapeView*)gViewServer->DoCreateViews(this, NULL, kShapeViewRSRCID, gZeroVPt);
  620.             FailNIL(shapeView);
  621.             fShapeView = shapeView;
  622.             this->AddPrintHandlerToView(fShapeView);
  623.         }
  624.         else
  625.             this->CreateProceduralShapeView();
  626.     }
  627.  
  628.     // not for printing
  629.     else
  630.     {
  631.         TWindow* aWindow = NULL;
  632.         if (qTemplateViews)
  633.         {
  634.             aWindow = gViewServer->NewTemplateWindow(kShapeWindowRSRCID, this);
  635.             FailNIL(aWindow);
  636.             fShapeView = (TShapeView*)aWindow->FindSubView(kShapeClipType);
  637.             FailNIL(fShapeView);
  638.             this->AddPrintHandlerToView(fShapeView);
  639.         }
  640.         else
  641.         {
  642.             this->CreateProceduralShapeView();
  643.             aWindow = gViewServer->NewSimpleWindow(kShapeWindowRSRCID, 
  644.                                                    kWantHScrollBar, kWantVScrollBar,
  645.                                                    this, fShapeView);
  646.             FailNIL(aWindow);
  647.         }
  648.  
  649.         if (fReopening)
  650.             this->RestoreWindow(aWindow);
  651.         else
  652.         {
  653.             aWindow->AdaptToScreen();
  654.             CPoint stagger(kStaggerAmount, kStaggerAmount);
  655.             aWindow->SimpleStagger(stagger, gStaggerCount);
  656.         }
  657.  
  658.         // Set window's resize limits so it can't become wider than the shapeView's edge
  659.         CPoint minSize, maxSize;
  660.         minSize = aWindow->fResizeLimits[topLeft];
  661.         maxSize = aWindow->fResizeLimits[botRight];
  662.         maxSize.h = (short)Min(fShapeView->fSize.h + kSBarSizeMinus1, maxSize.h);
  663.         aWindow->SetResizeLimits(minSize, maxSize);
  664.     }
  665.     
  666.     ShapesBeInView(fShapeView);
  667. }
  668.  
  669. //----------------------------------------------------------------------------------------
  670. // TShapeDocument::RestoreWindow
  671. //----------------------------------------------------------------------------------------
  672. #pragma segment AOpen
  673.  
  674. void TShapeDocument::RestoreWindow(TWindow* aWindow)
  675. {
  676.     // Restore the window and scroller using the settings in the document's fDocState field
  677.     aWindow->Locate(fDocState.theLocation, false);
  678.     aWindow->Resize(fDocState.theSize, false);
  679.     aWindow->ForceOnScreen();
  680.     fShapeView->GetScroller(false)->SetLocalOrigin(fDocState.theScrollPosition, false);
  681. }
  682.  
  683. //----------------------------------------------------------------------------------------
  684. // TShapeDocument::CreateProceduralShapeView
  685. //----------------------------------------------------------------------------------------
  686. #pragma segment AOpen
  687.  
  688. void TShapeDocument::CreateProceduralShapeView()
  689. {
  690.     TShapeView* shapeView = new TShapeView;
  691.     shapeView->IShapeView(this, false);
  692.     fShapeView = shapeView;
  693. }
  694.  
  695. //----------------------------------------------------------------------------------------
  696. // TShapeDocument::DoSetupMenus
  697. //----------------------------------------------------------------------------------------
  698. #pragma segment ARes
  699.  
  700. void TShapeDocument::DoSetupMenus()    // Override
  701. {
  702.     Inherited::DoSetupMenus();
  703.  
  704.     for (CommandNumber i=cArrow; i <= cHBox; i++)
  705.         Enable(i, true);
  706.     
  707.     Enable(1901, true);
  708.     Enable(1902, true);
  709.     Enable(1903, true);
  710. }
  711.  
  712. //----------------------------------------------------------------------------------------
  713. // TShapeDocument::DoNeedDiskSpace
  714. //    Doc file has the following format:
  715. //        Data Fork:
  716. //        (a)  kSizePrintInfo (120 bytes) => PrintInfo
  717. //        (b)  The rest => The shapes themselves
  718. //    
  719. //         Resource Fork:
  720. //        (a)  SIZEOF(DocState) => DocState (rsrc type: 'DSTA', number: 1)
  721. //    
  722. //----------------------------------------------------------------------------------------
  723. #pragma segment AWriteFile
  724.  
  725. void TShapeDocument::DoNeedDiskSpace(TFile* itsFile,
  726.                                      long& dataForkBytes,
  727.                                      long& rsrcForkBytes)    // Override
  728. {
  729.     // Get Print record requirements
  730.     Inherited::DoNeedDiskSpace(itsFile, dataForkBytes, rsrcForkBytes);
  731.  
  732.     dataForkBytes = dataForkBytes + fShapeList->GetSize() + sizeof(ShapeData);
  733.     rsrcForkBytes = rsrcForkBytes + kRsrcTypeOverhead + kRsrcOverhead + sizeof(DocState);
  734. }
  735.  
  736. //----------------------------------------------------------------------------------------
  737. // TShapeDocument::DoRead
  738. //----------------------------------------------------------------------------------------
  739. #pragma segment AReadFile
  740.  
  741. void TShapeDocument::DoRead(TFile* aFile, Boolean forPrinting)    // Override
  742. {
  743.     Inherited::DoRead(aFile, forPrinting);
  744.  
  745.     // Read in doc data
  746.     TFileStream* aFileStream = new TFileStream;
  747.     aFileStream->IFileStream(aFile);
  748.     TContext* itsContext = new TContext;
  749.     itsContext->IContext();
  750.     aFileStream->SetContext(itsContext);
  751.     this->ReadFrom(aFileStream);
  752.     FreeIfObject(aFileStream);
  753. }
  754.  
  755. //----------------------------------------------------------------------------------------
  756. // TShapeDocument::ReadFrom
  757. //----------------------------------------------------------------------------------------
  758. #pragma segment AReadFile
  759.  
  760. void TShapeDocument::ReadFrom(TStream* aStream)    // Override
  761. {
  762.     Inherited::ReadFrom(aStream);
  763.  
  764.     // Read in the doc state
  765.     aStream->ReadBytes(&fDocState, sizeof(DocState));
  766.  
  767.     // Read in the shapes
  768.     TShape* newShape = NULL;
  769.     for (short i=0; i < fDocState.theNumberOfShapes; i++)
  770.     {
  771.         if (aStream->ReadStreamObject((TObject * &)newShape))
  772.             this->AddShape(newShape);
  773. #if qDebug
  774.         else
  775.             fprintf(stderr, "###TShapeDocument::ReadFrom: Can’t read in a shape");
  776. #endif
  777.     }
  778. }
  779.  
  780. //----------------------------------------------------------------------------------------
  781. // TShapeDocument::DoWrite
  782. //----------------------------------------------------------------------------------------
  783. #pragma segment AWriteFile
  784.  
  785. void TShapeDocument::DoWrite(TFile* aFile, Boolean makingCopy)    // Override
  786. {
  787.     Inherited::DoWrite(aFile, makingCopy);
  788.  
  789.     TFileStream* aFileStream = new TFileStream;
  790.     aFileStream->IFileStream(aFile);
  791.     TContext* itsContext = new TContext;
  792.     itsContext->IContext();
  793.     aFileStream->SetContext(itsContext);
  794.     this->WriteTo(aFileStream);
  795.     FreeIfObject(aFileStream);
  796. }
  797.  
  798. //----------------------------------------------------------------------------------------
  799. // TShapeDocument::WriteTo
  800. //----------------------------------------------------------------------------------------
  801. #pragma segment AWriteFile
  802.  
  803. void TShapeDocument::WriteTo(TStream* aStream)    // Override
  804. {
  805.     short numberOfShapes;
  806.     CRect dummyRect;
  807.     DocState localDocState;
  808.  
  809.     Inherited::WriteTo(aStream);
  810.  
  811.     // Write out the doc state
  812.     this->SurveyShapes(false, numberOfShapes, dummyRect);
  813.     TWindow* window = (TWindow*)fWindowList->First();
  814.     localDocState.theLocation = window->fLocation;
  815.     localDocState.theSize = window->fSize;
  816.     localDocState.theScrollPosition = fShapeView->GetScroller(false)->fTranslation;
  817.     localDocState.theNumberOfShapes = numberOfShapes;
  818.     aStream->WriteBytes(&localDocState, sizeof(DocState));
  819.  
  820.     // Write out the shapes
  821.     this->EachVirtualShapeDo(&WriteShape, (void*)aStream);
  822. }
  823.  
  824. //----------------------------------------------------------------------------------------
  825. // TShapeDocument::ShapesAsPict
  826. //----------------------------------------------------------------------------------------
  827. #pragma segment AWriteFile
  828.  
  829. PicHandle TShapeDocument::ShapesAsPict(TList* aShapeList)
  830. {
  831.     TShapeView* clipShapeView;
  832.     TShapeDocument* clipShapeDoc;
  833.     TShape*    aNewShape;
  834.     
  835.     clipShapeDoc = new TShapeDocument;
  836.     clipShapeDoc->IShapeDocument(NULL);
  837.  
  838.     clipShapeView = new TShapeView;
  839.     clipShapeView->IShapeView(clipShapeDoc, true);
  840.     VPoint itsSize(fShapeView->fSize.h, fShapeView->fSize.v);
  841.     clipShapeView->Resize(itsSize, kDontRedraw);
  842.     clipShapeDoc->fShapeView = clipShapeView;
  843.  
  844.     for (ArrayIndex i=0; i < aShapeList->GetSize(); i++)
  845.     {
  846.         aNewShape = (TShape*)aShapeList->At(i)->Clone();
  847.         clipShapeDoc->AddShape(aNewShape);
  848.     }
  849.  
  850.     FreeIfObject(clipShapeDoc);                        // Get rid of temp document
  851.     return clipShapeView->AsPict();
  852. }
  853.  
  854. //----------------------------------------------------------------------------------------
  855. // TShapeDocument::SurveyShapes
  856. //----------------------------------------------------------------------------------------
  857. #pragma segment ShapeRes
  858.  
  859. void TShapeDocument::SurveyShapes(Boolean selecteesOnly, short& numberOfShapes, CRect& combinedExtent)
  860. {
  861.     numberOfShapes = 0;
  862.     combinedExtent = gZeroRect;
  863.     SurveyRec surveyRecord(selecteesOnly, numberOfShapes, combinedExtent);
  864.  
  865.     this->EachVirtualShapeDo(&UnionizeShapes, (void*)&surveyRecord);
  866. }
  867.  
  868. //----------------------------------------------------------------------------------------
  869. // TShapeDocument::ShapeUnderMouse
  870. //----------------------------------------------------------------------------------------
  871. #pragma segment ShapeRes
  872.  
  873. TShape* TShapeDocument::ShapeUnderMouse(CPoint qdPt)
  874. {
  875.     CheckShapeRec checkRec(qdPt);
  876.     EachVirtualShapeDo(&CheckShape, (void*)&checkRec);
  877.     return checkRec.fShapeUnderMouse;
  878. }
  879.  
  880. //----------------------------------------------------------------------------------------
  881. // TShapeDocument::DeselectShapes
  882. //----------------------------------------------------------------------------------------
  883. #pragma segment ShapeRes
  884.  
  885. void TShapeDocument::DeselectShapes()
  886. {
  887.     EachPotentialShapeDo(&DeselShape, NULL);
  888. }
  889.  
  890. //----------------------------------------------------------------------------------------
  891. // TShapeDocument::RestoreSelection
  892. //----------------------------------------------------------------------------------------
  893. #pragma segment ShapeRes
  894.  
  895. void TShapeDocument::RestoreSelection(TShapeView* shapeView)
  896. {
  897.     EachPotentialShapeDo(&DoRestore, (void*)shapeView);
  898. }
  899.  
  900. //----------------------------------------------------------------------------------------
  901. // TShapeDocument::DrawShapes
  902. //----------------------------------------------------------------------------------------
  903. #pragma segment ShapeRes
  904.  
  905. void TShapeDocument::DrawShapes(CRect& qdArea, Boolean dragging)
  906. {
  907.     DrawShapeRec drawRec(qdArea, dragging);
  908.     EachVirtualShapeDo(&DrawShape, (void*)&drawRec);
  909. }
  910.  
  911. //----------------------------------------------------------------------------------------
  912. // TShapeDocument::HiliteShapes
  913. //----------------------------------------------------------------------------------------
  914. #pragma segment ShapeRes
  915.  
  916. void TShapeDocument::HiliteShapes(TShapeView* shapeView, HLState fromHL, HLState toHL)
  917. {
  918.     HiliteShapeRec hlShapeRec(shapeView, fromHL, toHL);
  919.     EachVirtualShapeDo(&HiliteShape, (void*)&hlShapeRec);
  920. }
  921.  
  922. //----------------------------------------------------------------------------------------
  923. // TShapeDocument::SaveSelection
  924. //----------------------------------------------------------------------------------------
  925. #pragma segment ShapeRes
  926.  
  927. void TShapeDocument::SaveSelection(TShapeView* shapeView, Boolean andInval)
  928. {
  929.     SaveSelRec saveRec(shapeView, andInval);
  930.     EachPotentialShapeDo(&SaveSel, (void*)&saveRec);
  931. }
  932.  
  933. //----------------------------------------------------------------------------------------
  934. // TShapeDocument::SelectAllShapes
  935. //----------------------------------------------------------------------------------------
  936. #pragma segment ShapeRes
  937.  
  938. void TShapeDocument::SelectAllShapes(TShapeView* shapeView)
  939. {
  940.     EachVirtualShapeDo(&SelectIt, shapeView);
  941. }
  942.  
  943. //----------------------------------------------------------------------------------------
  944. // TShapeDocument::CursorInShape
  945. //----------------------------------------------------------------------------------------
  946. #pragma segment ShapeRes
  947.  
  948. Boolean TShapeDocument::CursorInShape(CPoint qdPoint, RgnHandle cursorRegion)
  949. {
  950.     Boolean cursorSet = false;
  951.     TestCursorRec testRec(cursorSet, qdPoint, cursorRegion);
  952.     EachVirtualShapeDo(&TestShapeCursor, (void*)&testRec);
  953.  
  954.     return cursorSet;
  955. }
  956.  
  957. //----------------------------------------------------------------------------------------
  958. // TShapeDocument::AnyShapesSelected
  959. //----------------------------------------------------------------------------------------
  960. #pragma segment ShapeRes
  961.  
  962. void TShapeDocument::AnyShapesSelected(Boolean& anySelection, Boolean& anyShapes)
  963. {
  964.     TestShapesRec testRec(anySelection, anyShapes, IsFiltering());
  965.     EachVirtualShapeDo(&TestShapes, (void*)&testRec);
  966. }
  967.  
  968. //----------------------------------------------------------------------------------------
  969. // TShapeDocument::ShapesBeInView
  970. //----------------------------------------------------------------------------------------
  971. #pragma segment ShapeRes
  972.  
  973. void TShapeDocument::ShapesBeInView(TShapeView* shapeView)
  974. {
  975.     EachShapeDo(&ShapeBeInView, shapeView);
  976. }
  977.  
  978. //----------------------------------------------------------------------------------------
  979. // TShapeDocument::MakeClipView: Launch a view to represent the data found
  980. //    in the Clipboard at application start-up time, or when switching back in, or when
  981. //    returning from a Desk Accessory.
  982. //----------------------------------------------------------------------------------------
  983. #pragma segment AClipboard
  984.  
  985. TView* TShapeDocument::MakeClipView()
  986. {
  987.     TShapeView*    clipShapeView = new TShapeView;
  988.     clipShapeView->IShapeView(this, true);
  989.     
  990.     fShapeView = clipShapeView;
  991.     
  992.     fShapeView->ReadFromDeskScrap();
  993.     
  994.     return clipShapeView;
  995. }
  996.  
  997.